package main

import (
	"flag"
	"fmt"
	"log"
	"os"

	"github.com/sideshow/apns2"
	"github.com/sideshow/apns2/certificate"
)

func main() {

	token := flag.String("token", "", "device token")
	bSandbox := flag.Bool("sandbox", false, "use sandbox p12 file")
	bAP := flag.Bool("ap", false, "wethere is ap region")
	content := flag.String("content", "", "notify content")
	flag.Parse()
	if *token == "" || *content == "" {
		flag.Usage()
		os.Exit(1)
	}
	p12file := "ZGame_Apple_Push_Services_-_2023_09_06.p12"
	if *bSandbox == true {
		p12file = "ZGame_APNs_Development_iOS_2023_09_06.p12"
	}
	if *bAP {
		p12file = "ZGame_AP_Apple_Push_Services_-_2024_02_26.p12"
		if *bSandbox == true {
			p12file = "ZGame_AP_APNs_Development_iOS_2024_04_09.p12"
			//	p12file = "ZGame_AP_APNs_Development_iOS_2024_02_26.p12"
		}
		fmt.Println("is ap")
	}

	fmt.Println(p12file)
	cert, err := certificate.FromP12File(p12file, "moonton123")
	if err != nil {
		log.Fatal("Cert Error:", err)
	}

	notification := &apns2.Notification{}
	//notification.DeviceToken = "ad407903e693b5a1d1ce710ed35e2e22c719ee0b892242fa354de7f8a33d8557"
	notification.DeviceToken = *token
	//notification.DeviceToken = "6c6d0e6b2fb44824ef0453b03e7a395eed01919665c8aa16403bbdf43fc3d8c8"
	//notification.DeviceToken = "50e97b80a25296c1dbf2fe6abe399cfeffd6763964a63987eb084ded55b3d3ef"
	notification.Topic = "com.td.watcherofrealms"
	if *bAP {
		//notification.Topic = "com.ap.watcherofrealms"
		//notification.Topic = "4W4HQPY7TC"
	}
	payload := `{"aps":{"alert":"` + *content
	payload += `"}}`
	//notification.Payload = []byte(`{"aps":{"alert":"*content `"}}`) // See Payload section below
	notification.Payload = []byte(payload) // See Payload section below

	// If you want to test push notifications for builds running directly from XCode (Development), use
	// client := apns2.NewClient(cert).Development()
	// For apps published to the app store or installed as an ad-hoc distribution use Production()

	client := apns2.NewClient(cert).Production()
	if *bSandbox {
		client = apns2.NewClient(cert).Development()
	}
	res, err := client.Push(notification)

	if err != nil {
		log.Fatal("Error:", err)
	}

	fmt.Printf("%v %v %v, %+v\n", res.StatusCode, res.ApnsID, res.Reason, res)
}
